package com.google.project.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.project.client.Service.ProjectService;
import com.google.project.server.entity.Project;
import com.google.project.server.entity.ProjectTask;
import com.google.project.shared.CommonFuntion;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.SubConstructorDTO;


@SuppressWarnings("serial")
public class ProjectServiceImpl extends RemoteServiceServlet implements
		ProjectService {
	/*
	 * add new project
	 */
	@Override
	public ProjectDTO addProject(ProjectDTO projectDTO) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Project project = null;
		try {
			project = new Project(projectDTO);
			pm.makePersistent(project);
		
		} finally {
			pm.close();
		}
		return project.toDTO();
	}

	/*
	 * get all project
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectDTO> getProjectList() {
		List<ProjectDTO> projectList = new ArrayList<ProjectDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Project.class);
		try {
			List<Project> list = (List<Project>) q.execute();
			for (Project project : list) {
				projectList.add(project.toDTO());
			}
		} catch (Exception e) {
			// logger.warning(e.getMessage());
		} finally {
			pm.close();
		}
		return projectList;

	}
	/*
	 * Delete project
	 */
	@Override
	public void deleteProject(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Project project = pm.getObjectById(Project.class, id);
			if (project != null) {
				pm.deletePersistent(project);
			}
		} finally {
			pm.close();
		}
	}

	/*
	 * Update project
	 */
	@Override
	public void updateProject(ProjectDTO projectDTO) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		Project project = null;
		try {
			project = pm
					.getObjectById(Project.class, projectDTO.getProjectID());
			project.updateFromDTO(projectDTO);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	/*
	 * getInfo by ProjectID
	 */
	//@Override
	public ProjectDTO getProjectById(String projectId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		ProjectDTO result = new ProjectDTO();
		Project projectInfo;
		try {
			projectInfo = pm.getObjectById(Project.class, projectId);
			result=projectInfo.toDTO();
		} finally {
			pm.close();
		}
		
		
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectDTO> searchProjectList(int status, String projectName,
			Long startDate, Long finishDate) {
		List<ProjectDTO> projectList = new ArrayList<ProjectDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String whereStr = "";
		// status
		try {
			if (status != 0) {
				whereStr += " status == " + status;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// projectName
		if (projectName != null && !projectName.equalsIgnoreCase("")) {
			if (whereStr != "") {
				whereStr += " && projectName ==  \"" + projectName + "\"";
			} else if (whereStr.equalsIgnoreCase("")) {
				whereStr += " projectName ==  \"" + projectName + "\"";
			}
		}

		// Them dieu kien ve thoi gian
		if (startDate != null) {
			if (whereStr != "") {
				whereStr += " && startDate >=  " + startDate;
			} else if (whereStr.equalsIgnoreCase("")) {
				whereStr += " startDate >=  " + startDate;
			}
		}

		if (finishDate != null) {
			if (whereStr != "") {
				whereStr += " && dueDate <=  " + finishDate;
			} else if (whereStr.equalsIgnoreCase("")) {
				whereStr += " dueDate <=  " + finishDate;
			}
		}

		Query q;
		if (whereStr != "") {
			q = pm.newQuery(Project.class, whereStr);
		} else {
			q = pm.newQuery(Project.class);
		}

		try {
			List<Project> list = (List<Project>) q.execute();
			for (Project project : list) {
				projectList.add(project.toDTO());
			}
		} catch (Exception e) {
		} finally {
			pm.close();
		}
		return projectList;
	}

	@Override
	public List<SubConstructorDTO> getListSubContructor(String projectId) {
		// TODO Auto-generated method stub
		List<SubConstructorDTO> list = new ArrayList<SubConstructorDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Project projectInfo;
		try {
			projectInfo = pm.getObjectById(Project.class, projectId);
			if (projectInfo.getListOfSubContructor() != null) {
				for (int i = 0; i < projectInfo.getListOfSubContructor().size(); i++)
					list.add(projectInfo.getListOfSubContructor().get(i)
							.toDTO());
			}

		} finally {
			pm.close();
		}

		return list;
	}

	@Override
	public ProjectDTO getProjectObject(String projectId) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Project projectInfo = null;
		try {
			projectInfo = pm.getObjectById(Project.class, projectId);
			projectInfo.getListOfSubContructor();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}

		return projectInfo.toDTOFullData();
	}

	@Override
	public List<ProjectTaskDTO> getListTask(String projectId) {
		// TODO Auto-generated method stub
		List<ProjectTaskDTO> list = new ArrayList<ProjectTaskDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Project projectInfo;
		try {
			projectInfo = pm.getObjectById(Project.class, projectId);
			if (projectInfo.getListOfTask() != null) {
				for (int i = 0; i < projectInfo.getListOfTask().size(); i++)
					list.add(projectInfo.getListOfTask().get(i).toDTO());
			}
    
		}catch(Exception ex){System.out.print(ex.getMessage());} finally {
			pm.close();
		}
		return list;
	}

	@Override
	public void deleteProjectList(List<ProjectDTO> list) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			for (ProjectDTO projectDTO : list) {
				Project project = pm.getObjectById(Project.class, projectDTO
						.getProjectID());
				if (project != null) {
					pm.deletePersistent(project);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}

	}

	@Override
	public void updateProjectForClose(ProjectDTO data)
	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ProjectStaffServiceImpl projectStaff = new ProjectStaffServiceImpl();
			
			Project project = pm.getObjectById(Project.class,data.getProjectID());
			if(data.getStatus()==3)
			{
				for(ProjectTask task : project.getListOfTask())
				{
					if(task.getStatus()!=3&&task.getStatus()!=2)
					{
					task.setStatus(3);
					task.setFinishDate(CommonFuntion.removeTime(new Date()));
				//	pm.makePersistent(task);
					}
				}
			}
			project.getListOfStaff();
			//projectStaff
			projectStaff.updateForCloseProject(project.getListOfStaff());
			//canchange
			//project.setFinishDate(CommonFuntion.removeTime(new Date(project.getDueDate())));
			//canUse
			project.setFinishDate(CommonFuntion.removeTime(new Date()));
			project.setStatus(data.getStatus());
			//pm.makePersistent(project);
			
		}catch(Exception ex){
			System.out.println(ex.getMessage());
	} finally {
		pm.close();
	}
		
		//for()
	}

}
